/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IFormDokter.java
 *
 * Created on 15 Jun 10, 6:02:40
 */
package clinsoftbeta.GUI;

import clinsoftbeta.database.Database;
import com.mysql.jdbc.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Sidiq Prihatno
 */
public class IFormDokter extends javax.swing.JInternalFrame {

    DefaultTableModel tbModel;
    String query = "";
    private Statement st;
    private Database db;
    private ResultSet rs;
    String modeOperasi = "";

    /** Creates new form IFormDokter */
    public IFormDokter() {
        initComponents();
        txtNamaDokter.setDocument(new CumaHuruf());
        setVisible(true);
        db = new Database();
        db.koneksiDatabase();
        isikanTabel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        bg1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtKodeDokter = new javax.swing.JTextField();
        txtNamaDokter = new javax.swing.JTextField();
        txtTmptLahirDok = new javax.swing.JTextField();
        txtTglLahirDok = new javax.swing.JTextField();
        rbLakiLakiDok = new javax.swing.JRadioButton();
        rbPerempuanDok = new javax.swing.JRadioButton();
        txtKotaDok = new javax.swing.JTextField();
        txtAlamatDok = new javax.swing.JTextField();
        txtNoTelpDok = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        btnHapus = new javax.swing.JButton();
        btnBersih = new javax.swing.JButton();
        btnSimpan = new javax.swing.JButton();
        btnBatal = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbDokter = new javax.swing.JTable();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Input Data Dokter");

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel6.setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("Kode Dokter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel4, gridBagConstraints);

        jLabel5.setText("Nama Dokter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel5, gridBagConstraints);

        jLabel6.setText("Tempat Lahir");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Tanggal Lahir");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Jenis Kelamin");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Alamat");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel9, gridBagConstraints);

        jLabel10.setText("Kota");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel10, gridBagConstraints);

        jLabel12.setText("No. Telepon");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel12, gridBagConstraints);

        txtKodeDokter.setPreferredSize(new java.awt.Dimension(250, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtKodeDokter, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtNamaDokter, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtTmptLahirDok, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtTglLahirDok, gridBagConstraints);

        bg1.add(rbLakiLakiDok);
        rbLakiLakiDok.setText("Laki - Laki");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(rbLakiLakiDok, gridBagConstraints);

        bg1.add(rbPerempuanDok);
        rbPerempuanDok.setText("Perempuan");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(rbPerempuanDok, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtKotaDok, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtAlamatDok, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtNoTelpDok, gridBagConstraints);

        jPanel4.add(jPanel6);

        jPanel1.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 20, 20));

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        jPanel7.add(btnHapus);

        btnBersih.setText("Bersih");
        btnBersih.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBersihActionPerformed(evt);
            }
        });
        jPanel7.add(btnBersih);

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        jPanel7.add(btnSimpan);

        btnBatal.setText("Batal");
        btnBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatalActionPerformed(evt);
            }
        });
        jPanel7.add(btnBatal);

        jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane3.setAutoscrolls(true);

        tbDokter.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Kode Dokter", "Nama", "Tempat Lahir", "Tgl. Lahir", "Jenis Kelamin", "Alamat", "Kota", "No. Telepon"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbDokter.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDokterMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbDokter);

        jPanel2.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void isikanTabel() {
        String[] tbHeader = {"Kode Dokter", "Nama", "tempat_lahir", "tanggal_lahir", "jenis_kelamin", "alamat", "kota", "no_telepon"};
        tbModel = new DefaultTableModel(null, tbHeader);
//        kosongkanTabel();
        try {
            query = "Select kode_dokter, nama_dokter, tempat_lahir,tanggal_lahir,jenis_kelamin, alamat, kota, no_telepon from dokter";
            st = (Statement) db.koneksi.createStatement();
            rs = st.executeQuery(query);
            while (rs.next()) {
                String[] tbData = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8)};
                tbModel.addRow(tbData);
            }
        } catch (SQLException se) {
            se.printStackTrace();
            //System.exit(0);
        }
        tbDokter.setModel(tbModel);
        if (tbDokter.getRowCount() > 0) {
            tbDokter.addRowSelectionInterval(0, 0);
            //      tampilkanDiText();
//            aktifText(false);
//            AktifSimpan(false);
        } else {
            //btnBaruActionPerformed(null);
        }
    }
    private void btnBersihActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBersihActionPerformed
        // TODO add your handling code here:
        bg1.clearSelection();
        txtKodeDokter.setText("");
        txtNamaDokter.setText("");
        txtTglLahirDok.setText("");
        txtTmptLahirDok.setText("");
        txtAlamatDok.setText("");
        txtNoTelpDok.setText("");
        txtKotaDok.setText("");
}//GEN-LAST:event_btnBersihActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        // TODO add your handling code here:
        if (txtKodeDokter.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Data ada yang masih kosong");
        }
        String JK;
        if (rbLakiLakiDok.isSelected()) {
            JK = "L";
        } else {
            JK = "P";
        }
        try {
            query = "INSERT INTO dokter VALUES('" + txtKodeDokter.getText() + "','"
                    + txtNamaDokter.getText() + "','" + txtTmptLahirDok.getText() + "','" + txtTglLahirDok.getText() + "','" + JK + "','"
                    + txtAlamatDok.getText() + "','" + txtKotaDok.getText() + "','" + txtNoTelpDok.getText() + "')";
            PreparedStatement presttmt = db.koneksi.prepareStatement(query);
            presttmt.executeUpdate(query);
            JOptionPane.showMessageDialog(null, "Data berhasil disimpan");
            //dispose();
            tbDokter.revalidate();
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                    "Terjadi kesalahan input data! error :" + se,
                    "Kesalahan", JOptionPane.ERROR_MESSAGE);
            // dispose();
        }
    
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void btnBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBatalActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnBatalActionPerformed
    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        // TODO add your handling code here:
        if (txtKodeDokter.getText().trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Data ada yang masih kosong");


}
            try {
                query = "delete from dokter where kode_dokter ='" + txtKodeDokter.getText() + "'";
                PreparedStatement

presttmt = db.koneksi.prepareStatement(query);
                presttmt.

executeUpdate(query);
                JOptionPane.

showMessageDialog(null, "Data berhasil di hapus");
                //dispose();
                //tbDaftar.revalidate();


} catch (SQLException se) {
                JOptionPane.showMessageDialog(null,
                        "Terjadi kesalahan hapus data! error :" + se,
                        "Kesalahan", JOptionPane.ERROR_MESSAGE);
                dispose(

);


}
        
    }//GEN-LAST:event_btnHapusActionPerformed

    private void tbDokterMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDokterMouseClicked
        // TODO add your handling code here:
           if (evt.getClickCount() == 1) {
            Tampil();


}
    }
    int row = 0;

    public void

Tampil() {
        row = tbDokter.getSelectedRow();
        txtKodeDokter.

setText(tbDokter.getValueAt(row, 0).toString());
        txtNamaDokter.

setText(tbDokter.getValueAt(row, 1).toString());
        txtTmptLahirDok.

setText(tbDokter.getValueAt(row, 2).toString());
        txtTglLahirDok.

setText(tbDokter.getValueAt(row, 3).toString());


if (tbDokter.getValueAt(row, 4).toString().equals("L")) {
            rbLakiLakiDok.setSelected(true);



} else {
            rbPerempuanDok.setSelected(true);


}
        txtAlamatDok.setText(tbDokter.getValueAt(row, 5).toString());
        txtKotaDok.

setText(tbDokter.getValueAt(row, 6).toString());
        txtNoTelpDok.

setText(tbDokter.getValueAt(row, 7).toString());
        
    }//GEN-LAST:event_tbDokterMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bg1;
    private javax.swing.JButton btnBatal;
    private javax.swing.JButton btnBersih;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JRadioButton rbLakiLakiDok;
    private javax.swing.JRadioButton rbPerempuanDok;
    private javax.swing.JTable tbDokter;
    private javax.swing.JTextField txtAlamatDok;
    private javax.swing.JTextField txtKodeDokter;
    private javax.swing.JTextField txtKotaDok;
    private javax.swing.JTextField txtNamaDokter;
    private javax.swing.JTextField txtNoTelpDok;
    private javax.swing.JTextField txtTglLahirDok;
    private javax.swing.JTextField txtTmptLahirDok;
    // End of variables declaration//GEN-END:variables

}
